﻿using System;
using System.Data;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

using ClassLib.Froms.Connection;

namespace ClassLib.Classes.Connection
{
    /// <summary>
    /// Summary description for ClsConnection.
    /// </summary>
    public class ClsConnection
    {
        private static Chuoi.EncryptDecrypt ed = new Chuoi.EncryptDecrypt();

        public delegate void DBChanger();
        public static event DBChanger DBEventChanging;
        public static event DBChanger DBEventChanged;

        private static MySqlConnection mySqlConn;
        public static MySqlTransaction MySQLTransaction = null;

        private static MySqlConnectionStringBuilder myCSB = new MySqlConnectionStringBuilder();

        public static string Username
        {
            get
            {
                return ed.DecryptText(global::ClassLib.Properties.Settings.Default.m_username);
            }
            set
            {
                global::ClassLib.Properties.Settings.Default.m_username = ed.EncryptText(value);
                global::ClassLib.Properties.Settings.Default.Save();
            }
        }

        public static string Password
        {
            get
            {
                return ed.DecryptText(global::ClassLib.Properties.Settings.Default.m_password);
            }
            set
            {
                global::ClassLib.Properties.Settings.Default.m_password = ed.EncryptText(value);
                global::ClassLib.Properties.Settings.Default.Save();
            }
        }

        public static string Server
        {
            get
            {
                return ed.DecryptText(global::ClassLib.Properties.Settings.Default.m_server);
            }
            set
            {
                global::ClassLib.Properties.Settings.Default.m_server = ed.EncryptText(value);
                global::ClassLib.Properties.Settings.Default.Save();
            }
        }

        public static string Database
        {
            get
            {
                return ed.DecryptText(global::ClassLib.Properties.Settings.Default.m_database);
            }
            set
            {
                global::ClassLib.Properties.Settings.Default.m_database = ed.EncryptText(value);
                global::ClassLib.Properties.Settings.Default.Save();
            }
        }

        public static string ConnectionString
        {
            get
            {
                return global::ClassLib.Properties.Settings.Default.DBConnectionString;
            }
            set
            {
                global::ClassLib.Properties.Settings.Default.DBConnectionString = value;
            }
        }

        public static string MySqlConnectionString
        {
            get
            {
                string username = ed.DecryptText(global::ClassLib.Properties.Settings.Default.m_username);
                string csdl = ed.DecryptText(global::ClassLib.Properties.Settings.Default.m_database);
                string host = ed.DecryptText(global::ClassLib.Properties.Settings.Default.m_server);
                string password = ed.DecryptText(global::ClassLib.Properties.Settings.Default.m_password);

                return string.Format("Server = {0}; Data Source = {1}; UID = {2}; Password = {3}; port = 3306,charset=utf8, Convert Zero Datetime = True", host, csdl, username, password);
            }
        }

        public static MySqlConnection MySqlConn
        {
            get
            {
                //nếu chưa nối kết thì thực hiện nối kết
                while (mySqlConn == null || mySqlConn.State != ConnectionState.Open)
                {

                    if (global::ClassLib.Properties.Settings.Default.m_username == null
                        || global::ClassLib.Properties.Settings.Default.m_username.Trim() == "")
                    {
                        if (MessageBox.Show("Không mở được file cấu hình!\nBạn có muốn cấu hình server không?",
                            "Thông báo", MessageBoxButtons.YesNo,
                            MessageBoxIcon.Question,
                            MessageBoxDefaultButton.Button2)
                            == DialogResult.Yes)
                        {
                            if (DBEventChanging != null)
                                DBEventChanging();
                            FrmCauHinhCSDL frmChSQL = new FrmCauHinhCSDL();
                            frmChSQL.ShowDialog();
                            if (DBEventChanged != null)
                                DBEventChanged();
                            continue;
                        }
                        else
                        {
                            mySqlConn = null;
                            Environment.Exit(0);
                            break;
                        }
                    }

                    string username = ed.DecryptText(global::ClassLib.Properties.Settings.Default.m_username);
                    string csdl = ed.DecryptText(global::ClassLib.Properties.Settings.Default.m_database);
                    string host = ed.DecryptText(global::ClassLib.Properties.Settings.Default.m_server);
                    string password = ed.DecryptText(global::ClassLib.Properties.Settings.Default.m_password);

                   try
                    {
                        myCSB.Server = host;
                        myCSB.Database = csdl;
                        myCSB.UserID = username;
                        myCSB.Password = password;
                        myCSB.CharacterSet = "utf8";
                        //myCSB.AllowZeroDateTime = true;
                        //mySqlConn.ConnectionString = string.Format("Server = {0};database={1};Uid={2};Pwd={3}", host, csdl, username, password);
                        //mySqlConn.Open();

                        //myCSB.Pooling = true;
                        //myCSB.MinimumPoolSize = 1;
                        //myCSB.MaximumPoolSize = 99;
                        //myCSB.ConnectionReset = true;
                        //myCSB.ConnectionLifeTime = 7200;
                        //myCSB.ConnectionLifeTime = 0;

                        mySqlConn = new MySqlConnection(myCSB.ConnectionString);

                        if (mySqlConn.State == ConnectionState.Open)
                            mySqlConn.Close();
                        
                        if (mySqlConn.State == ConnectionState.Closed)
                            mySqlConn.Open();

                     

                    }
                   catch (Exception e)
                   {
                      // MessageBox.Show(e.Message);
                       if (MessageBox.Show("Không thể kết nối đến server!\nBạn có muốn cấu hình server không?",
                           "Thông báo", MessageBoxButtons.YesNo,
                           MessageBoxIcon.Question,
                           MessageBoxDefaultButton.Button2)
                           == DialogResult.Yes)
                       {
                           if (DBEventChanging != null)
                               DBEventChanging();
                           FrmCauHinhCSDL frmChSQL = new FrmCauHinhCSDL();
                           frmChSQL.ShowDialog();
                           if (DBEventChanged != null)
                               DBEventChanged();
                           mySqlConn = null;
                           continue;
                       }
                       else
                       {
                           mySqlConn = null;
                           Environment.Exit(0);
                           break;
                       }
                   }
                }
        
                return mySqlConn;
            }
            set
            {
                mySqlConn = value;
            }

        }


        public static string LocationSave
        {
            get
            {
                return global::ClassLib.Properties.Settings.Default.LocationSave;
            }
            set
            {
                global::ClassLib.Properties.Settings.Default.LocationSave = value;
                global::ClassLib.Properties.Settings.Default.Save();
            }
        }

    }
}
